﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using WebView.WSSite;

namespace WebView.Site {
	public partial class Default : System.Web.UI.Page {
	
		private SitesServices t;
	
		protected void Page_Load(object sender, EventArgs e) {
			t = new SitesServices();
			GridView1.DataSource = t.listar();
			this.refreshGrid();
		}

		protected void Button1_Click(object sender, EventArgs e) {
			Label3.Text = "Adicionar";
			HiddenField1.Value = null;
			Panel1.Visible = false;
			Panel2.Visible = true;
		}

		protected void Button2_Click(object sender, EventArgs e) {
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
			t.remover(id);
			this.refreshGrid();
		}

		protected void Button3_Click(object sender, EventArgs e) {
			string saveDir = @"\SitesRoot\";
			string appPath = Request.PhysicalApplicationPath;
			string savePath = appPath + saveDir + Server.HtmlEncode(TextBox1.Text);
			if (String.IsNullOrEmpty(HiddenField1.Value)) {
				t.adicionar(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text);	
				Directory.CreateDirectory(savePath);
		//	} else {
		//		t.atualizar(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text);
				
			}
			Panel1.Visible = true;
			Panel2.Visible = false;
			this.refreshGrid();
		}

		protected void Button4_Click(object sender, EventArgs e) {
			Panel1.Visible = true;
			Panel2.Visible = false;
		}

		protected void Button5_Click(object sender, EventArgs e) {
			Label3.Text = "Editar";
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
			//WebView.WSSite.Site o = t.get(id);
			HiddenField1.Value = id.ToString();
			//TextBox1.Text = o.nome;
			//TextBox2.Text = o.tipo;
			//TextBox3.Text = o.categoria;
			//TextBox4.Text = o.descricao;
			Panel1.Visible = false;
			Panel2.Visible = true;
			this.refreshGrid();
		}

		protected void Button6_Click(object sender, EventArgs e) {
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
			//t.duplicar(id);			
			this.refreshGrid();
		}

		private void refreshGrid() {
			GridView1.DataSource = t.listar();
			GridView1.DataBind();
		}

	}
}
